package com.bodacn.andontv;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import androidx.fragment.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;



public class StudyFragment extends Fragment {
    // TODO: Rename parameter arguments, choose names that match
    public static final int msgSaveDataOver=1001;
    public static String TAG=StudyFragment.class.getName();
    public static GridView callGrid;

    private static String RunState="TRUE";
    private static int ThisFragmentIndex=2;
    //用于与主Activity通讯;
    private Handler mainHandler;
    //分页显示;
    private JSONArray callArray;//用于存储WebService返回的结果;
    AndonAdapter adapter;

    public StudyFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //得到Activity的Handler,用于发消息给主Activity;
        MainActivity activity=(MainActivity) getActivity();
        mainHandler=activity.mainHandler;
        //加接口监听
        activity.setTransStudyParamsListener(new MainActivity.OnTransStudyParamsListener() {
            @Override
            public void TransParams(Map<String, Object> mParams) {
                if (mParams!=null) {
                    try {
                        Log.d("MSG","setTransStudyParamsListener----TransParams--1");
                        String paramComm= mParams.get("ParamComm").toString();
                        String paramValue= mParams.get("ParamValue").toString();
                        //根据参数显示订单信息;
                        if (paramComm=="CallAddr")
                        {
                            System.out.println("paramValue:"+paramValue);
                            if (callArray==null) callArray=new JSONArray();
                            for (int i=0;i<callArray.length();i++){
                                if (callArray.optJSONObject(i).has("CallAddr")) {
                                    if (callArray.optJSONObject(i).getString("CallAddr").equals(paramValue)) {
                                        Toast.makeText(getContext(), "重复添加", Toast.LENGTH_LONG).show();
                                        return;
                                    }
                                }
                            }
                            JSONObject jsonObject=new JSONObject();
                            jsonObject.put("CallAddr",paramValue);
                            callArray.put(jsonObject);
                            callGrid.setAdapter(adapter);
                        }
                        Log.d("MSG","setTransStudyParamsListener----TransParams--2");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_study, container, false);
        InitView(root);//按钮初始化;
        System.out.println(TAG+"_onCreateView");
        return root;
    }

    //界面初始化;
    private void InitView(View v) {
        callGrid=v.findViewById(R.id.callGrid);
        adapter=new AndonAdapter(getContext());
    }




    //数据适配器，继承自BaseAdapter;
    private final class AndonAdapter extends BaseAdapter {
        private LayoutInflater inflater;
        public AndonAdapter(Context context) {
            inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
        public  String TrnasValue(String params)
        {
            if ((params.toUpperCase()=="NULL".toUpperCase())||(params=="0")||(params=="0.0"))
                return "";
            else return params;
        }
        @Override
        public int getCount() {
            //固定行数;
            return callArray.length();
        }
        @Override
        public Object getItem(int position) {
            try {
                return callArray.getJSONArray(position);
            } catch (JSONException e)
            {
                e.printStackTrace();
                System.out.println("getItem取JSON数据出错!");
                return  null;
            }
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = inflater.inflate(R.layout.study_item, null, false);
            try {
                TextView tv = view.findViewById(R.id.item_CallIndex);// 显示文字
                tv.setText(String.valueOf(position));
                tv = view.findViewById(R.id.item_CallAddr);// 显示文字
                tv.setText(callArray.optJSONObject(position).getString("CallAddr"));
            }  catch (JSONException e1) {
                e1.printStackTrace();
            }
            return view;
        }
    }


    //通过消息来执行处理类程序,不更新UI则不一定需要通过这个;
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // TODO Auto-generated method stub
            // 根据消息ID来判断要做的事情
            int msgId = msg.what;
            switch (msgId) {
                case msgSaveDataOver:

                    break;
                default:
                    break;
            }
        }
    };

}
